import dayjs from 'dayjs';
import { isEmpty, pennyToDollar } from '@/utils/util';
import { DispatchStatus, OrderStatus } from './constant';

export function dateFormat(str : string) {
	if (isEmpty(str)) return str;
	return dayjs(str).format('YYYY.MM.DD')
}

export function dateTimeFormat(str : string) {
	if (isEmpty(str)) return str;
	return dayjs(str).format('YYYY-MM-DD HH:mm')
}

export function moneyComman(first : string) {
	const commaCount = Math.ceil(first.length / 3);
	const firstReversed = first.split('').reverse();
	const firstList = [];
	for (let i = 0; i < commaCount; i++) {
		firstList.push(firstReversed.slice(i * 3, (i + 1) * 3).reverse().join(''))
	}
	first = firstList.reverse().join(',');
	return first;
}

export function pennyToDollarFormat(num : number, comma = false) : string {
	let [first, last = ''] = String(pennyToDollar(num)).split('.');
	if (comma) {
		first = moneyComman(first);
	}
	return `${first}.${last.padEnd(2, '0')}`;
}

export function moneyFormat(num : number, comma = false) : string {
	let [first, last = ''] = String(num).split('.');
	if (comma) {
		first = moneyComman(first);
	}
	return `${first}.${last.padEnd(2, '0')}`;
}

export function dispatchStatusFormat(dispatchStatus : DispatchStatus) {
	switch (dispatchStatus) {
		case DispatchStatus.Pending:
			return '待处理';
		case DispatchStatus.Processing:
			return '待接单';
		case DispatchStatus.Confirmed:
			return '处理中';
		case DispatchStatus.Completed:
			return '申请完成';
		case DispatchStatus.Finished:
			return '已完成';
		case DispatchStatus.Returned:
			return '已退回';
	}
	return '';
}

export function orderStatusFormat(orderStatus : OrderStatus) {
	switch (orderStatus) {
		case OrderStatus.Pending:
			return '待处理';
		case OrderStatus.Paid:
			return '已付款';
		case OrderStatus.Processing:
			return '处理中';
		case OrderStatus.Completed:
			return '已完成';
		case OrderStatus.Cancelled:
			return '已取消';
		case OrderStatus.Refunded:
			return '已退款';
	}
	return '';
}